<?php

class Db_Joins_UsersJoins
{	
	var $admin_id;
	var $page_id;
	var $page_size = 30;
	var $skip_pagination;
	var $keyword;
	
	function __construct() {
		$this->db = Zend_Registry::get("db");
	}
	
	function GetAdminList()
	{
		$result = array();
		
		//count total for pagination
		$data = $this->db->select();
		$data->from("admin-users", array("count(admin_id) as ct"));
		$ct = $this->db->fetchRow($data);

		//dump all in condition
		$data = $this->db->select();
		$data->from("admin-users as a", array("admin_id", "admin_email", "admin_name", "admin_status"));
		$data->joinLeft("admin-groups as g", "g.group_id=a.admin_group_id", array("group_name"));
		if($this->keyword)
		{
			$data->where("a.admin_email like '%".$this->keyword."%' or a.admin_name like '%".$this->keyword."%'");
		}
		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		$data->order("a.admin_id DESC");
		$rows = $this->db->fetchAll($data);
		
		$result = array(
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $rows
		);
		
		return $result;
	}
	
	function GetAdminInfo()
	{
		$result = array();
		
		if($this->admin_id)
		{
			$data = $this->db->select();
			$data->from("admin-users", array("admin_id", "admin_email", "admin_group_id", "admin_name", "admin_status"));
			$data->where("admin_id = ?", $this->admin_id);
			$result = $this->db->fetchRow($data);
		}
		
		return $result;
	}
	
	function GetMemberList()
	{
		$result = array();
		
		//count total for pagination
		$data = $this->db->select();
		$data->from("members", array("count(member_id) as ct"));
		$ct = $this->db->fetchRow($data);

		//dump all in condition
		$data = $this->db->select();
		$data->from("members", array("cellphone", "nick_name", "gender", "email", "ctime"));
		if($this->keyword)
		{
			$data->where("cellphone like '%".$this->keyword."%' or nick_name like '%".$this->keyword."%' or email like '%".$this->keyword."%'");
		}
		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		$data->order("member_id DESC");
		$rows = $this->db->fetchAll($data);

		$result = array(
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $rows
		);
		
		return $result;
	}
	
	function GetAgentList()
	{
		$result = array();
		
		//count total for pagination
		$data = $this->db->select();
		$data->from("agents", array("count(*) as ct"));
		$ct = $this->db->fetchRow($data);

		//dump all in condition
		$data = $this->db->select();
		$data->from("agents as a", array("cellphone", "company", "contact_name", "gender", "ctime", "email"));
		$data->joinLeft("industry-list as i", "i.industry_id=a.industry_id", "industry_name");
		$data->joinLeft("company-ages as g", "g.age_id=a.company_age_id", "age_name");
		$data->joinLeft("company-scales as s", "s.scale_id=a.company_scale_id", "scale_name");
		if($this->keyword)
		{
			$data->where("cellphone like '%".$this->keyword."%' or company like '%".$this->keyword."%' or contact_name like '%".$this->keyword."%'");
		}
		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		$data->order("a.agent_id DESC");
		$rows = $this->db->fetchAll($data);

		$result = array(
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $rows
		);
		
		return $result;
	}
}